﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TFAB50
    {

        public TFAB50()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABAUFM();
            DataContext = MV;
            InitializeComponent();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            //ABAMANDANT =:ABAMANDANT: AND ABAVKAUFKRS =:ABAVKAUFKRS: AND ABAVKAUFLFD =:ABAVKAUFLFD: AND ABAVKAUFLFD =:ABAVKAUFPOS:
            dwData = TBMain.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVKAUFKRS"] = EXAUF.Substring(0, 2);
            dwData["ABAVKAUFLFD"] = EXAUF.Substring(2, 6);
            dwData["ABAVKAUFPOS"] = EXAUF.Substring(8, 3);
            TMV.QMU07(dwData);
            base.OnEXAUFChg(vlaue);
        } 
        private void btnSaveBatch_Click(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            UpdateBatches(Batches);
        }
        decimal Batches
        {
            get
            {
                if (txtBatches.Text.Trim() == "")
                    txtBatches.Text = "0";
                return Convert.ToDecimal(txtBatches.Text.ToString());
            }
            set
            {
                txtBatches.Text = value.ToString();
            }
        }
        private void UpdateBatches(decimal batches)
        {
            string ABAAPOS = "";
            try
            {
                decimal ABARESMG = Convert.ToDecimal(dwMaster["ABARESMG"].ToString());
                decimal targetmqty = Math.Round(ABARESMG / Batches, 0);
                dwMaster["ABASANZSTK"] = batches;
                dwMaster["ABASSTKGEW"] = targetmqty;
                ABAAPOS = dwMaster["ABAAPOS"].ToString();
                filter = "ABAAUFKRS='" + dwMaster["ABAAUFKRS"].ToString() + "' AND ABAAUFLFD='" + dwMaster["ABAAUFKRS"].ToString() + "' AND ABAAUFPOS='" + dwMaster["ABAAUFPOS"].ToString() + "' AND ABAAPOS='" + dwMaster["ABAAPOS"].ToString() + "'";
                rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ABASANZSTK"] = batches;//分缸数
                    row["ABASSTKGEW"] = targetmqty;//单盘重量
                    TMV.UMU12(row); ;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
